/*
 * DialogoNumero.java
 *
 * Created on 15 de agosto de 2008, 0:13
 */

package l2bot.interfaz.dialogs;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import l2bot.Idioma;

/**
 *
 * @author  carl
 */
public class DialogoNumero extends javax.swing.JDialog {

    /** Creates new form DialogoNumero */
    boolean ma;
    int min,max,pre,ste;
    
    boolean cancel;
    String pregu;
    
    
    public DialogoNumero(java.awt.Frame parent, boolean modal, String preg, int minimo, int maximo, int predet,int step) {
        super(parent, modal);
        
        if(maximo < 0){
        ma = false;
        }else{
        ma = true;
        max = maximo;
        }
        
        pre = predet;
        min = minimo;
        max = maximo;
        ste = step;
        pregu = preg;
        
        
            
        initComponents();
        
        setVisible(true);
    }
  /*  public DialogoNumero(java.awt.Frame parent, boolean modal, int minimo, int maximo, int predet) {
        this(parent,modal,minimo,maximo,predet,1);
    }
    public DialogoNumero(java.awt.Frame parent, boolean modal, int minimo, int predet) {
        this(parent,modal,minimo,-1,predet,1);
    }*/
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    
    private void initComponents() {

        label1 = new java.awt.Label();
        jSpinner1 = new javax.swing.JSpinner();
        
        if(ma){
            jSlider1 = new javax.swing.JSlider(JSlider.HORIZONTAL,min,max,pre);
            jSlider1.setMajorTickSpacing(10);
            jSlider1.setMinorTickSpacing(1);
            jSlider1.setPaintTicks(true);
            jSlider1.setPaintLabels(true);
        }else
        {
            jSlider1 = new javax.swing.JSlider();
        }
        
        button1 = new java.awt.Button();
        button2 = new java.awt.Button();
        button3 = new java.awt.Button();
        button4 = new java.awt.Button();
        button5 = new java.awt.Button();
        button6 = new java.awt.Button();
        button7 = new java.awt.Button();
        button8 = new java.awt.Button();
        button9 = new java.awt.Button();
        button10 = new java.awt.Button();
        button11 = new java.awt.Button();
        button12 = new java.awt.Button();
        button13 = new java.awt.Button();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setResizable(false);
        
        
        SpringLayout layout = new SpringLayout();
        

        
        Container pane = this.getContentPane();

        label1.setText(pregu);
        SpinnerModel Model = new SpinnerNumberModel(pre,min,(Comparable)(ma?max:null),ste);  //step
        jSpinner1.setModel(Model);
        
        layout.putConstraint(SpringLayout.WEST, label1,10,SpringLayout.WEST, pane);
        layout.putConstraint(SpringLayout.NORTH, label1, 15,SpringLayout.NORTH, pane);
        
        layout.putConstraint(SpringLayout.WEST, jSpinner1,10,SpringLayout.WEST, pane);
        layout.putConstraint(SpringLayout.NORTH, jSpinner1, 5,SpringLayout.SOUTH, label1);      
        
        layout.putConstraint(SpringLayout.WEST, jSlider1,10,SpringLayout.WEST, pane);
        layout.putConstraint(SpringLayout.NORTH, jSlider1, 5,SpringLayout.SOUTH, jSpinner1);       
        
        
        
        layout.putConstraint(SpringLayout.WIDTH, jSlider1, 0, SpringLayout.WIDTH, jSpinner1);
        layout.putConstraint(SpringLayout.WIDTH, label1, 0, SpringLayout.WIDTH, jSpinner1);
        
        layout.putConstraint(SpringLayout.WIDTH, jSpinner1, -50, SpringLayout.WEST, button1);
        
        
        layout.putConstraint(SpringLayout.EAST, button3,-10,SpringLayout.EAST, pane);
        layout.putConstraint(SpringLayout.NORTH, button3, 5,SpringLayout.NORTH,pane);        
        
        layout.putConstraint(SpringLayout.EAST, button6,-10,SpringLayout.EAST, pane);
        layout.putConstraint(SpringLayout.NORTH, button6, 5,SpringLayout.SOUTH,button3); 
        
        layout.putConstraint(SpringLayout.EAST, button9,-10,SpringLayout.EAST, pane);
        layout.putConstraint(SpringLayout.NORTH, button9, 5,SpringLayout.SOUTH,button6); 
        
        layout.putConstraint(SpringLayout.EAST, button2,-5,SpringLayout.WEST, button3);
        layout.putConstraint(SpringLayout.NORTH, button2, 0,SpringLayout.NORTH,button3);        
        
        layout.putConstraint(SpringLayout.EAST, button1,-5,SpringLayout.WEST, button2);
        layout.putConstraint(SpringLayout.NORTH, button1, 0,SpringLayout.NORTH,button3);       
        
        layout.putConstraint(SpringLayout.EAST, button4,0,SpringLayout.EAST, button1);
        layout.putConstraint(SpringLayout.NORTH, button4, 0,SpringLayout.NORTH,button6);        
        
        layout.putConstraint(SpringLayout.EAST, button5,0,SpringLayout.EAST, button2);
        layout.putConstraint(SpringLayout.NORTH, button5, 0,SpringLayout.NORTH,button6);  

        layout.putConstraint(SpringLayout.EAST, button7,0,SpringLayout.EAST, button1);
        layout.putConstraint(SpringLayout.NORTH, button7, 0,SpringLayout.NORTH,button9);        
        
        layout.putConstraint(SpringLayout.EAST, button8,0,SpringLayout.EAST, button2);
        layout.putConstraint(SpringLayout.NORTH, button8, 0,SpringLayout.NORTH,button9); 
        
        
        layout.putConstraint(SpringLayout.EAST, button10,0,SpringLayout.EAST, button3);
        layout.putConstraint(SpringLayout.NORTH, button10, 5,SpringLayout.SOUTH,button9);        
        
        layout.putConstraint(SpringLayout.WEST, button11,0,SpringLayout.WEST, button1);
        layout.putConstraint(SpringLayout.NORTH, button11, 0,SpringLayout.NORTH,button10); 
        
        
        layout.putConstraint(SpringLayout.EAST, button11, -5,SpringLayout.WEST,button10); 
        
        
        layout.putConstraint(SpringLayout.NORTH, button12,15, SpringLayout.SOUTH,jSlider1);
        layout.putConstraint(SpringLayout.WEST, button12,15, SpringLayout.WEST,pane);
        
        layout.putConstraint(SpringLayout.NORTH, button13,15, SpringLayout.SOUTH,jSlider1);
        layout.putConstraint(SpringLayout.WEST, button13,15, SpringLayout.EAST,button12);       
        
        pane.setPreferredSize(new Dimension(500,150));

        button12.setLabel(Idioma.aceptar);
        button13.setLabel(Idioma.cancelar);
        
        
        button1.setLabel("1");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });
        
        button12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setVisible(false);
            }
        });
        
        button13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel = true;
                setVisible(false);
            }
        });
        

        button2.setLabel("2");
        button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button2ActionPerformed(evt);
            }
        });

        button3.setLabel("3");
        button3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button3ActionPerformed(evt);
            }
        });

        button4.setLabel("4");
        button4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button4ActionPerformed(evt);
            }
        });

        button5.setLabel("5");
        button5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button5ActionPerformed(evt);
            }
        });

        button6.setLabel("6");
        button6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button6ActionPerformed(evt);
            }
        });

        button7.setLabel("7");
        button7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button7ActionPerformed(evt);
            }
        });

        button8.setLabel("8");
        button8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button8ActionPerformed(evt);
            }
        });

        button9.setLabel("9");
        button9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button9ActionPerformed(evt);
            }
        });

        button10.setLabel("0");
        button10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button10ActionPerformed(evt);
            }
        });
        
        button11.setLabel("all");
        button11.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSpinner1.setValue(max);
                jSlider1.setValue(max);
            }
        });

        add(label1);
        add(jSpinner1);
        
        
        
        add(button1);
        add(button2);
        add(button3);
        add(button4);
        add(button5);
        add(button6);
        add(button7);
        add(button8);
        add(button9);
        add(button10);
        add(button12);
        add(button13);
        pane.setLayout(layout);
        
        if(ma){
            
            jSlider1.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent evt) {
                    jSpinner1.setValue((int)jSlider1.getValue());
                }
            });
            
            jSpinner1.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent evt) {
                    jSlider1.setValue(((Integer)jSpinner1.getValue()).intValue());
                }
            });
            
            
            
        add(button11);
        add(jSlider1);
        
        }
        

        pack();
    }

private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
insertar(1);
}

private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
insertar(2);
}

private void button6ActionPerformed(java.awt.event.ActionEvent evt) {
insertar(6);
}

private void button3ActionPerformed(java.awt.event.ActionEvent evt) {
insertar(3);
}

private void button4ActionPerformed(java.awt.event.ActionEvent evt) {
insertar(4);
}

private void button5ActionPerformed(java.awt.event.ActionEvent evt) {
insertar(5);
}

private void button7ActionPerformed(java.awt.event.ActionEvent evt) {
insertar(7);
}

private void button8ActionPerformed(java.awt.event.ActionEvent evt) {
insertar(8);
}

private void button9ActionPerformed(java.awt.event.ActionEvent evt) {
insertar(9);
}

private void button10ActionPerformed(java.awt.event.ActionEvent evt) {
insertar(0);
}

public int getValue(){
    return ((Integer)jSpinner1.getValue()).intValue();
}


void insertar(int n){
    int a = ((Integer)jSpinner1.getValue()).intValue();
    String nu = a==0?""+n:a+""+n;
    if(nu.length() > 10){return;}     // 2.147.000.000
    int nn = Integer.parseInt(nu);
    if(ma && nn>max){
        return;
    }
    jSpinner1.setValue(nn);
    if(ma){
    jSlider1.setValue(nn);
    }
}

    /**
    * @param args the command line arguments
    */


    // Variables declaration - do not modify
    private java.awt.Button button1;
    private java.awt.Button button10;
    private java.awt.Button button11;
    private java.awt.Button button2;
    private java.awt.Button button3;
    private java.awt.Button button4;
    private java.awt.Button button5;
    private java.awt.Button button6;
    private java.awt.Button button7;
    private java.awt.Button button8;
    private java.awt.Button button9;
    private Button button12;
    private Button button13;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JSpinner jSpinner1;
    private java.awt.Label label1;
    // End of variables declaration

}